<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/10/16
 * Time: 14:20
 */
namespace app\api\model;
use think\Controller;
use think\Model;
use think\Db;
use app\api\model\Hotel;
use app\api\model\Tour;
class Food extends Model
{
    /*
     * 首页吃啥
     */
    public function eat($page,$latitude,$longitude)
    {
        $res = Db::name('food_store')
                ->field('food_store_id,food_store_cover,food_store_name,food_people_money,food_tag,food_store_longitude,food_store_latitude')
                ->where('is_del',0)
                ->where('is_pass',1)
                ->where('is_tuijian',1)
                ->order('sort','desc')
                ->limit(($page-1)*config('page_size'),config('page_size'))
                ->select();
        foreach($res as $k =>$v)
        {
            $res[$k]['food_store_cover'] = addWaterByField($v['food_store_cover']);
            $res[$k]['range'] = getDistance($latitude,$longitude,$v['food_store_latitude'],$v['food_store_longitude']);
            if(empty($v['food_tag'])){
                $res[$k]['food_tag'] = [];
            }else{
                $res[$k]['food_tag'] = explode(',',$v['food_tag']);
            }
        }
        return $res;
    }
    /*
     * 美食店铺列表
     */
    public function food_list($where,$page,$status='',$longitude='',$latitude='')
    {
        $res =  Db::name('food_store')
            ->field('food_store_id,food_store_name,food_store_cover,food_people_money,food_tag,food_store_longitude,food_store_latitude')
            ->where('is_pass',1)
            ->where('is_del',0)
            ->where($where)
            ->limit(($page-1)*config('page_size'),config('page_size'))
            ->select();

        foreach($res as $k =>$v)
        {
            $res[$k]['food_store_cover'] =  addWaterByField($v['food_store_cover']);
            $res[$k]['range'] = getDistance($latitude,$longitude,$v['food_store_latitude'],$v['food_store_longitude']);
            if(empty($v['food_tag'])){
                $res[$k]['food_tag'] = [];
            }else{
                $res[$k]['food_tag'] = explode(',',$v['food_tag']);
            }
        }
        if($status==1){
            //根据某个值对二维数组排序
            $last_names = array_column($res,'range');
            array_multisort($last_names,SORT_ASC,$res);
        }else if($status==2){
            $last_names = array_column($res,'food_people_money');
            array_multisort($last_names,SORT_ASC,$res);
        }
        return $res;
    }
    
    /**
     * [getFoodStoreInfo 获取美食店铺的详细信息]
     * @Author   kendy
     * @DateTime 2019-10-30T10:04:01+0800
     * @param    [type]                   $food_store_id [description]
     * @return   [type]                                  [description]
     */
    public function getFoodStoreInfo($food_store_id,$longitude='',$latitude='')
    {
        $resultData = Db::name('food_store')
        ->field('food_store_id,food_store_name,food_people_money,food_store_cover,food_tag,food_store_address,food_store_address,food_store_tel,store_show,food_store_time,city,food_store_content,food_store_longitude,food_store_latitude')
        ->where(['is_del'=>0,'is_pass'=>1,'food_store_id'=>$food_store_id])
        ->find();
        if(!empty($resultData))
        {
            $hot = new Hotel();
            $tour = new Tour();
            $resultData['food_store_cover'] =  addWaterByField($resultData['food_store_cover']);
            $resultData['food_tag'] = explode(',', $resultData['food_tag']); 
            $resultData['food_store_address'] =  $resultData['city'].$resultData['food_store_address'];
            //美食图片
            $imgArrObj = Db::name('food_store_img')
            ->field('id,img_path')
            ->where('store_id',$resultData['food_store_id'])
            ->select();
            if(count($imgArrObj)>0){
              foreach ($imgArrObj as $key => $value) {
                 $imgArr[] = $value['img_path'];
              }
              $resultData['food_store_image'] = $imgArr;
            }
            //美食展示
            $resultData['food_list'] = Db::name('food')
            ->field('food_id,food_title,food_cover')
            ->where('store_id',$resultData['food_store_id'])
            ->select();
          //获取附近民俗显示距离最近的三个 
               $resultData['nearby_hotel'] = $hot->hotel_list(1,1,4,$resultData['food_store_longitude'],$resultData['food_store_latitude'],3);
               foreach ($resultData['nearby_hotel'] as $key => &$value) { 
                  unset($value['latitude']);
                  unset($value['longitude']);
               } 
          //附近景点
              $resultData['nearby_tours'] = $tour->tour_list(1,1,4,$resultData['food_store_longitude'],$resultData['food_store_latitude'],3);
               foreach ($resultData['nearby_tours'] as $key => &$value) {
                   unset($value['latitude']);
                   unset($value['longitude']);
               }  
            return $resultData;
        }
        return $resultData; 
    }

    /**
     * @param $post
     */
    public function insert_food($post)
    {
        $res = Db::name('food_store')->insertGetId($post);
        if($res){
            return $res;
        }else{
            return false;
        }
    }
}